﻿using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_TrayBusiness : BaseBusiness<PB_Tray>, IPB_TrayBusiness, ITransientDependency
    {
        
        #region 外部接口

        

        public async Task<PB_Tray> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }


        [DataEditLog(UserLogType.托盘管理, "Code", "托盘")]
        [DataRepeatValidate(new string[] { "TrayTypeId", "Code" }, new string[] { "托盘类型", "编码" }, allData: false, matchOr: false)]
        public async Task UpdateDataAsync(PB_Tray data)
        {
            await UpdateAsync(data);
        }

        public  async  Task<List<PB_TrayDTO>> Selectid(PB_Trayid pB_Trayid) {
            Expression<Func<PB_Tray, PB_TrayType, PB_Location,Base_Department, PB_TrayDTO>> select = (a, b, c,d) => new PB_TrayDTO
            {
                TypeNmae = b.Name,
                LocionNmae = c.Name,
                WorkShopName = d.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_TrayType>() on a.TrayTypeId equals b.Code into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<PB_Location>() on a.LocalId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    join d in Db.GetIQueryable<Base_Department>() on a.WorkShopId equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    select @select.Invoke(a, b, c,d);
            string[] arr = pB_Trayid.id.Split(',');
            List<string> List = new List<string>();
            foreach (var item in arr)
            {
                List.Add(item);
            }
            q = q.Where(x => List.Contains(x.Id));
            var res = await q.ToListAsync();
            return res;
        }

        public async Task EnableTheData(string id)
        {
            await UpdateAsync(w => w.Id == id, entity => { entity.Status = 1; });
        }

        public async Task DisableTheData(string id)
        {
            await UpdateAsync(w => w.Id == id, entity => { entity.Status = 0; });
        }

        [DataDeleteLog(UserLogType.托盘管理, "Code", "托盘")]
        public async Task DeleteDataAsync(List<string> ids)
        {
            List<PB_TrayCirculationBill> TrayCirculationBillList = await Db.GetIQueryable<PB_TrayCirculationBill>().Where(x => ids.Contains(x.TrayId)).ToListAsync();
            if (TrayCirculationBillList.Count > 0)
            {
                throw new BusException("已被引用不能删除");
            }
            await DeleteAsync(ids);
        }

        public async Task AddDataExlAsync(List<PB_Tray> list)//void
        {
            await InsertAsync(list);   //  BulkInsert    InsertAsync    

        }

        public IQueryable<T> GetQueryable<T>() where T : class, new()
        {
            return Db.GetIQueryable<T>();
        }
        #endregion

        #region 私有成员

        #endregion
    }
}